﻿function findRotation(x1,y1,x2,y2)
  local t = -math.deg(math.atan2(x2-x1,y2-y1))
  if t < 0 then t = t + 360 end;
  return t;
end

g_Timestamp = 1325372400 -- Nowy rok
launched = false
dbg = false

function formatDate(d) -- Konwersja sekundy -> dni, godziny, minuty, sekundy
h_raw = math.floor(d / 3600)
m = math.floor(d % 3600 / 60)
s = math.floor(d % 3600 % 60)
h = ((h_raw/24) - math.floor(h_raw/24))*24
days = math.floor(h_raw/24)
if #tostring(days) < 2 then
	days = "0"..tostring(days)
elseif #tostring(h) < 2 then
	h = "0"..tostring(h)
elseif #tostring(m) < 2 then
	m = "0"..tostring(m)
elseif #tostring(s) < 2 then
	s = "0"..tostring(s)
end
return days, h, m, s
end
-- (-2236.99634 -1735.00134 480.73840) -- centrum
function getX()
	local randX = math.random( -2247.6928,-2229.0996)
	return randX
end

function getY()
	local randY = math.random(  -1748.59241 , -1707.53186)
	return randY
end

function launch()
	if launched == false then
		launched = true
		outputChatBox("Puść własne fajerwerki! (/petarda)", root, 255,255,0)
		setPetardyUnlocked(true)
		--killTimer(tim)
		--setTimer(function ()
		--	launchPetarda(getX(), getY(), 482)
		--end,1000, 100)
	end
end

addCommandHandler("manualnie", launch)

function refresh()
	local realTimestamp = getRealTime().timestamp
	if ((g_Timestamp - realTimestamp) <= 0) or (dbg == true) then
		launch()
	end
end

tim = setTimer(refresh, 100,0)

-- x,y podestu
xpod=-2330.8999023438
ypod=-1616.3000488281

-- colshape
col=createColSphere(-2330.8999023438,-1616.3000488281,485,30)
addEventHandler("onColShapeHit",root,
	function(element)
		if source == col then
			local _type=getElementType(element)
			if(_type=="vehicle")then
			local occupants=getVehicleOccupants(element)
			local driver=occupants[0]
				if driver then
					if not hasObjectPermissionTo(driver,"general.adminpanel")then
						for k,v in pairs(occupants)do
							outputChatBox("* Po parkiecie nie jeździmy! >:C",v,150,50,50)
						end
						destroyElement(element)
					end
				end
			end
		end
	end
)
addEventHandler("onPlayerVehicleEnter",root,
	function(vehicle,seat)
		if isElementWithinColShape(vehicle,col)then
			if seat==0 and not hasObjectPermissionTo(source,"general.adminpanel")then
				outputChatBox("* Po parkiecie nie jeździmy! >:C",source,150,50,50)
				destroyElement(vehicle)
			end
		end
	end
)

-- drzewo
createObject(664,-2401.7492675781,-1632.5545654297,516.0791015625)
createObject(664,-2401.7490234375,-1632.5537109375,547.47924804688)
createObject(664,-2401.7490234375,-1632.5537109375,533.55920410156,0,348)
createObject(664,-2401.7490234375,-1632.5537109375,533.55920410156,0,347.99743652344,180)
createObject(664,-2401.7490234375,-1632.5537109375,533.55920410156,0,347.99743652344,90)
createObject(664,-2401.7490234375,-1632.5537109375,533.55920410156,0,347.99743652344,269.99450683594)
 
-- podest
createObject(3873,xpod,ypod,467.10000610352)

-- zolte swiatelka na podescie
createObject(7290,xpod,-1628.55078125,481.81649780273,90)
createObject(7290,-2318.5043945313,ypod,481.81555175781,90,0,90)
createObject(7290,-2330.8994140625,-1603.9951171875,481.80035400391,90,0,180)
createObject(7290,xpod,ypod,481.8623046875,90,0,269.99450683594)

-- niebieska gwiazda na podescie
n1=createObject(9127,xpod,ypod,483.09844970703)
n2=createObject(9127,xpod,ypod,483.09844970703,0,0,350)
n3=createObject(9127,xpod,ypod,483.09844970703,0,0,339.99694824219)
n4=createObject(9127,xpod,ypod,483.09844970703,0,0,329.99389648438)
n5=createObject(9127,xpod,ypod,483.09844970703,0,0,319.99084472656)
n6=createObject(9127,xpod,ypod,483.09844970703,0,0,309.98779296875)
n7=createObject(9127,xpod,ypod,483.09844970703,0,0,299.98474121094)
n8=createObject(9127,xpod,ypod,483.09844970703,0,0,289.98168945313)
n9=createObject(9127,xpod,ypod,483.09844970703,0,0,279.97863769531)
tabelaGwiazdy={n1,n2,n3,n4,n5,n6,n7,n8,n9}

-- torcze i ataczowanie
torcz1=createObject(3461,0,0,0)
attachElements(torcz1,n1,10,0,-0.3)
torcz2=createObject(3461,0,0,0)
attachElements(torcz2,n1,-10,0,-0.3)
torcz3=createObject(3461,0,0,0)
attachElements(torcz3,n1,9.5,0,-0.3)
torcz4=createObject(3461,0,0,0)
attachElements(torcz4,n1,-9.5,0,-0.3)

-- niebieskie pasy
niebieskiPas=createObject(5992,xpod,ypod,487.7692565918)

-- obracanie pasow i gwiazdy (czyli tez torczy)
a=0
setTimer(
	function()
		if(a==0)then
			moveObject(niebieskiPas,5000,xpod,ypod,487.7692565918,0,0,180)
			a=1
		elseif(a==1)then
			moveObject(niebieskiPas,5000,xpod,ypod,477.7692565918,0,0,180)
			a=0
		end
		for k,v in ipairs(tabelaGwiazdy)do
			moveObject(v,5000,xpod,ypod,483.09844970703,0,0,180)
		end
	end
,5000,0)

-- glosnik
--[[addCommandHandler("glosnik",
	function(pl)local 
		x,y,z=getElementPosition(pl)
		rx,ry,rz=getElementRotation(pl)
		setElementPosition(pl,x,y,z+5)
		createGlosnik(x,y,484.45150756836,rx,ry,rz)
		outputChatBox(x .. "," .. y .. "," .. 484.45150756836 .. "," .. "," .. rx .. "," .. ry .. "," ..rz)
	end
)]]
function createGlosnik(x,y,z,rx,ry,rz)
	local glosnik=createObject(1841,x,y,z,rx,ry,rz-90)
	local colGlosnik1=createObject(3798,x,y,z,rx,ry,rz)
	local colGlosnik2=createObject(3798,x,y,z+0.85,rx,ry,rz)
	setElementAlpha(colGlosnik1,0)
	setElementAlpha(colGlosnik2,0)
	setObjectScale(glosnik,6)
end

--[[-2311.8400878906,-1617.2982177734
function getPointFromDistanceRotation(x, y, dist, angle)
    local a = math.rad(90 - angle);
    local dx = math.cos(a) * dist;
    local dy = math.sin(a) * dist;
    return x+dx, y+dy;
end
function findRotation(x1,y1,x2,y2)
  local t = -math.deg(math.atan2(x2-x1,y2-y1))
  if t < 0 then t = t + 360 end;
  return t;
end
for i=1,8 do
 local x,y=getPointFromDistanceRotation(xpod,ypod,11,360*i/8)
 local rz=findRotation(x,y,xpod,ypod)
 createGlosnik(x,y,484.43,0,0,rz)
 outputChatBox(x .. "," .. y .. "," .. 484.45150756836 .. "," .. "," .. 0 .. "," .. 0 .. "," .. rz)
end
for i=1,8 do
 local x,y=getPointFromDistanceRotation(xpod,ypod,11,360*i/8)
 local rz=findRotation(x,y,xpod,ypod)
 createGlosnik(x,y,484.43+2.81,0,0,rz)
 outputChatBox(x .. "," .. y .. "," .. 484.45150756836 .. "," .. "," .. 0 .. "," .. 0 .. "," .. rz)
end]]

createGlosnik(-2323.1217277507,-1608.521874235,484.44,0,0,135)
createGlosnik(-2319.8999023438,-1616.3000488281,484.44,0,0,90)
createGlosnik(-2323.1217277507,-1624.0782234212,484.44,0,0,45)
createGlosnik(-2330.8999023438,-1627.3000488281,484.44,0,0,0)
createGlosnik(-2338.6780769369,-1624.0782234212,484.44,0,0,315)
createGlosnik(-2341.8999023438,-1616.3000488281,484.44,0,0,270)
createGlosnik(-2338.6780769369,-1608.521874235,484.44,0,0,225)
createGlosnik(-2330.8999023438,-1605.3000488281,484.44,0,0,180)
createGlosnik(-2323.1217277507,-1608.521874235,487.26,0,0,135)
createGlosnik(-2319.8999023438,-1616.3000488281,487.26,0,0,90)
createGlosnik(-2323.1217277507,-1624.0782234212,487.26,0,0,45)
createGlosnik(-2330.8999023438,-1627.3000488281,487.26,0,0,0)
createGlosnik(-2338.6780769369,-1624.0782234212,487.26,0,0,315)
createGlosnik(-2341.8999023438,-1616.3000488281,487.26,0,0,270)
createGlosnik(-2338.6780769369,-1608.521874235,487.26,0,0,225)
createGlosnik(-2330.8999023438,-1605.3000488281,487.26,0,0,180)

-- stanowisko DJ'a
createObject(2592,-2340.2019042969,-1620.1505126953,485.35772705078,0,0,112)
createObject(2592,-2340.708984375,-1620.3237304688,485.35772705078,0,0,291.99462890625)
createObject(14820,-2340.4665527344,-1620.2111816406,485.3567199707,0,0,292)

-- bar
createObject(16151,-2353.55078125,-1612.287109375,482.96771240234,0,0,163.99841308594)
createObject(3095,-2355.0668945313,-1611.7255859375,483.03656005859,0,270,163.99841308594)
createObject(7290,-2355.7265625,-1611.4741210938,484.53494262695,0,0,253.99841308594)

-- lampy cyrkusowe
createObject(3472,-2305.3498535156,-1650.6534423828,482.55383300781,0,0,0)
createObject(3472,-2316.349609375,-1656.4033203125,482.55383300781,0,0,0)
createObject(3472,-2311.349609375,-1665.1533203125,482.42364501953,0,0,0)
createObject(3472,-2299.349609375,-1661.1533203125,482.42364501953,0,0,0)
createObject(3472,-2295.349609375,-1671.1533203125,482.42364501953,0,0,0)
createObject(3472,-2304.849609375,-1676.4033203125,482.42364501953,0,0,0)
createObject(3472,-2300.099609375,-1684.9033203125,482.10855102539,0,0,0)
createObject(3472,-2291.099609375,-1680.4033203125,482.10855102539,0,0,0)
    
-- ozdóbki, pierdółki
createObject(3265,-2287.6892089844,-1713.2037353516,474.13424682617,0,0,190)
createObject(16778,-2354.927734375,-1618.5828857422,482.63549804688,0,0,190)
createObject(13722,-2346.4858398438,-1682.6770019531,489.68719482422,0,0,130)
createObject(13831,-2346.4853515625,-1682.6704101563,489.68121337891,0,0,129.99572753906)
createObject(11395,-2343.9741210938,-1589.9346923828,490.63909912109,0,0,20)
createObject(10838,-2389.6418457031,-1591.8773193359,518.02301025391,346,0,320)
createObject(3528,-2277.8708496094,-1722.4637451172,479.00247192383,0,0,190)
createObject(7073,-2265.0056152344,-1731.5485839844,504.56848144531,0,0,320)
createObject(7392,-2297.2409667969,-1724.5802001953,484.16204833984,18,0,80)
createObject(13562,-2370.7768554688,-1573.2686767578,498.73608398438,0,0,0)
createObject(2683,-2354.712890625,-1615.4248046875,483.71923828125,0,0,0)
createObject(2453,-2353.7458496094,-1615.4925537109,483.95147705078,0,0,30)
createObject(1209,-2352.3354492188,-1606.080078125,482.61456298828,0,0,60)
createObject(1775,-2351.57421875,-1605.1076660156,483.70892333984,0,0,40)
    

-- swiatelka
function choinkoweSwiatelka(bool)
	if(bool==true)or(bool=="true")or(bool==1)or(bool=="1")then
		if not(swiatelkaChoinkowe)then
			local s1=createMarker(-2401.7490234375,-1632.5537109375,596.21960449219,"corona",30,255,255,0,100)
			local s2=createMarker(-2391.6706542969,-1645.8054199219,560.53759765625,"corona",3,0,0,0,100)
			local s3=createMarker(-2383.9206542969,-1639.0554199219,560.53759765625,"corona",3,0,0,0,100)
			local s4=createMarker(-2381.6706542969,-1629.3054199219,559.28759765625,"corona",3,0,0,0,100)
			local s5=createMarker(-2381.4206542969,-1627.3054199219,562.03759765625,"corona",3,0,0,0,100)
			local s6=createMarker(-2383.4206542969,-1627.3054199219,549.28759765625,"corona",3,0,0,0,100)
			local s7=createMarker(-2382.4206542969,-1643.4349365234,544.53759765625,"corona",3,0,0,0,100)
			local s8=createMarker(-2382.9206542969,-1640.9349365234,531.4775390625,"corona",3,0,0,0,100)
			local s9=createMarker(-2380.9206542969,-1627.6849365234,531.4775390625,"corona",3,0,0,0,100)
			local s10=createMarker(-2389.6706542969,-1632.9349365234,538.2275390625,"corona",3,0,0,0,100)
			local s11=createMarker(-2391.1706542969,-1618.4349365234,529.7275390625,"corona",3,0,0,0,100)
			local s12=createMarker(-2388.6706542969,-1617.9349365234,546.9775390625,"corona",3,0,0,0,100)
			local s13=createMarker(-2392.6706542969,-1625.1849365234,553.7275390625,"corona",3,0,0,0,100)
			local s14=createMarker(-2388.1706542969,-1635.1849365234,550.9775390625,"corona",3,0,0,0,100)
			local s15=createMarker(-2390.1706542969,-1638.9349365234,542.7275390625,"corona",3,0,0,0,100)
			local s16=createMarker(-2398.6706542969,-1655.9543457031,544.7275390625,"corona",3,0,0,0,100)
			local s17=createMarker(-2413.6706542969,-1645.7043457031,530.4775390625,"corona",3,0,0,0,100)
			local s18=createMarker(-2394.4206542969,-1651.4543457031,532.4775390625,"corona",3,0,0,0,100)
			local s19=createMarker(-2387.9206542969,-1632.3054199219,572.53759765625,"corona",3,0,0,0,100)
			local s20=createMarker(-2392.1706542969,-1638.8054199219,574.03759765625,"corona",3,0,0,0,100)
			local s21=createMarker(-2397.6706542969,-1643.3054199219,571.03759765625,"corona",3,0,0,0,100)
			local s22=createMarker(-2395.5305175781,-1622.1862792969,571.03759765625,"corona",3,0,0,0,100)
			local s23=createMarker(-2395.5305175781,-1628.4362792969,579.53759765625,"corona",3,0,0,0,100)
			local s24=createMarker(-2395.5305175781,-1639.1862792969,581.03759765625,"corona",3,0,0,0,100)
			swiatelkaChoinkowe={s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24}
			choinkowyTimer=setTimer(
				function()
					if(swiatelkaChoinkowe)then
						for i=2,24 do
							local swiatelko=swiatelkaChoinkowe[i]
							local r=math.random(0,255)
							local g=math.random(0,255)
							local b=math.random(0,255)
							setMarkerColor(swiatelko,r,g,b,100)
						end
					end
				end
			,1000,0)
		end
	elseif(bool==false)or(bool=="false")or(bool==0)or(bool=="0")then
		if(swiatelkaChoinkowe)then
			killTimer(choinkowyTimer)
			for k,v in ipairs(swiatelkaChoinkowe)do
				destroyElement(v)
				swiatelkaChoinkowe=nil
			end
		end
	end
end
choinkoweSwiatelka(false)
addCommandHandler("swiatelka",
	function(gracz,cmd,bool)
		if(bool=="true")or(bool=="false")or(bool=="1")or(bool=="0")then
			choinkoweSwiatelka(bool)
		end
	end
)

urlElement = createElement("url") -- uzylem poprawnie :E
setElementData(urlElement, "url", "http://gr-relay-1.gaduradio.pl/2")

function ustawRadio(player, cmd, url)
	setElementData(urlElement, "url", url)
	triggerClientEvent("refreshRadio", root)
end

addCommandHandler("ustawRadio", ustawRadio)